package com.hd.trans.files.model;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import com.hd.trans.R;
import com.hd.trans.files.sort.ISort;

import java.util.ArrayList;
import java.util.List;

/**
 * 排序方式数据
 */
public class FileSortModel {
    public static int MODE_EDIT_TIME_NEW_TO_OLD = 0;
    public static int MODE_EDIT_TIME_OLD_TO_NEW = 1;
    public static int MODE_CREATED_TIME_NEW_TO_OLD = 2;
    public static int MODE_CREATED_TIME_OLD_TO_NEW = 3;
    public static int MODE_SIZE_SMALL_TO_LARGE = 4;
    public static int MODE_SIZE_LARGE_TO_SMALL = 5;
    public static int MODE_NAME_A_TO_Z = 6;
    public static int MODE_NAME_Z_TO_A = 7;
    private static List<ModeItem> modeList = new ArrayList<>();;

    public static List<ModeItem> getSortMode(Context context) {
        List<ModeItem> modes = new ArrayList<>();
        if(modeList!=null&&modeList.size()>0){
            return modeList;
        }
        String[] modeName = context.getResources().getStringArray(R.array.sort_files_item_name);
        modes.add(new ModeItem(MODE_EDIT_TIME_NEW_TO_OLD, modeName[0], true, ISort.SortMethod.changeTimeNew2Old));
        modes.add(new ModeItem(MODE_EDIT_TIME_OLD_TO_NEW, modeName[1], false, ISort.SortMethod.changeTimeOld2New));
        modes.add(new ModeItem(MODE_CREATED_TIME_NEW_TO_OLD, modeName[2], false, ISort.SortMethod.createTimeNew2Old));
        modes.add(new ModeItem(MODE_CREATED_TIME_OLD_TO_NEW, modeName[3], false, ISort.SortMethod.createTimeOld2New));
        modes.add(new ModeItem(MODE_SIZE_SMALL_TO_LARGE, modeName[4], false, ISort.SortMethod.sizeSmall2Big));
        modes.add(new ModeItem(MODE_SIZE_LARGE_TO_SMALL, modeName[5], false, ISort.SortMethod.sizeBig2Small));
        modes.add(new ModeItem(MODE_NAME_A_TO_Z, modeName[6], false, ISort.SortMethod.nameA2Z));
        modes.add(new ModeItem(MODE_NAME_Z_TO_A, modeName[7], false, ISort.SortMethod.nameZ2A));
        return modes;
    }

    public static void setModeName(List<ModeItem> modes){
        modeList.clear();
        modeList.addAll(modes);
    }

    public static class ModeItem implements Parcelable {
        private final int mode;
        private final String modeName;
        private boolean isSelect;
        private final ISort.SortMethod method;

        public ModeItem(int mode, String modeName, boolean isSelect, ISort.SortMethod method) {
            this.mode = mode;
            this.modeName = modeName;
            this.isSelect = isSelect;
            this.method = method;
        }

        protected ModeItem(Parcel in) {
            mode = in.readInt();
            modeName = in.readString();
            isSelect = in.readByte() != 0;
            method = in.readParcelable(ISort.SortMethod.class.getClassLoader());
        }

        public static final Creator<ModeItem> CREATOR = new Creator<ModeItem>() {
            @Override
            public ModeItem createFromParcel(Parcel in) {
                return new ModeItem(in);
            }

            @Override
            public ModeItem[] newArray(int size) {
                return new ModeItem[size];
            }
        };

        public int getMode() {
            return mode;
        }

        public String getModeName() {
            return modeName;
        }

        public ISort.SortMethod getMethod() {
            return method;
        }

        public boolean isSelect() {
            return isSelect;
        }

        public void setSelect(boolean select) {
            isSelect = select;
        }

        @Override
        public String toString() {
            return "ModeItem{" +
                    "mode=" + mode +
                    ", modeName='" + modeName + '\'' +
                    ", isSelect=" + isSelect +
                    ", method=" + method +
                    '}';
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(mode);
            dest.writeString(modeName);
            dest.writeByte((byte) (isSelect ? 1 : 0));
            dest.writeParcelable(method, flags);
        }
    }
}
